package com.e3.basdoc.bank.client;

import java.util.ArrayList;
import java.util.List;

import com.e3.basdoc.bank.model.BankFields;
import com.e3.basdoc.bank.model.BankModel;
import com.e3.basdoc.region.client.RegionRefDialog;
import com.e3.template.tgf.TGFHelper;
import com.e3.web.widget.client.MyFormFieldFactory;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;

public class BankHelper extends TGFHelper<BankModel>{
	
	private String[] notNullFieldNames = {BankModel.CODE
		,BankModel.NAME
		,BankModel.SHORT_NAME
		,BankModel.REGION
		};
	
	public static BankHelper INSTANCE = new BankHelper();
	
	public Field<?> createFormField(String name){
		Field<?> formField = null;
		String dispName = getFieldDisplayName(name);
		boolean isNull = isNotNullField(name);
		if(name.equals(BankFields.ID)){
			formField = MyFormFieldFactory.createIdField(name);
		}		
		else if(name.equals(BankFields.CODE)){
			formField = MyFormFieldFactory.createStringField(name,dispName,10,isNull);
		}
		else if(name.equals(BankFields.NAME)){
			formField = MyFormFieldFactory.createStringField(name,dispName,50,isNull);
		}
		else if(name.equals(BankFields.SHORT_NAME)){
			formField = MyFormFieldFactory.createStringField(name,dispName,20,isNull);
		}
		else if(name.equals(BankFields.ADDRESS)){
			formField = MyFormFieldFactory.createStringField(name,dispName,100,isNull);
		}
		else if(name.equals(BankFields.PARENT_BANK)){
			formField = MyFormFieldFactory.createRefField(name, dispName,new BankRefDialog(),isNull);
		}
		else if(name.equals(BankFields.REGION)){
			formField = MyFormFieldFactory.createRefField(name, dispName,new RegionRefDialog(),isNull);
		}
		else if(name.equals(BankFields.REMARK)){
			formField = MyFormFieldFactory.createTextAreaField(name, dispName,100,isNull);
		}
		return formField;
	}
	
	public List<ColumnConfig> getGridColumnConfigs(){
		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();
		RowNumberer numberer = new RowNumberer();   
		configs.add(numberer);
		
		ColumnConfig column = new ColumnConfig();
		String name = BankModel.CODE;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);  
	    
	    column = new ColumnConfig();
		name = BankModel.NAME;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);  
	    
	    column = new ColumnConfig();
		name = BankModel.SHORT_NAME;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);
	    
	    column = new ColumnConfig();
		name = BankModel.ADDRESS;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);
	    
	    column = new ColumnConfig();
		name = BankModel.PARENT_BANK;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column); 
	    
	    column = new ColumnConfig();
		name = BankModel.REGION;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);
	    
	    return configs;
	}
	
	public String getFieldDisplayName(String code) {
		return BankFieldsConstant.INSTANCE.getString(code);
	}
	
	public String[] getNotNullFieldNames(){
		return notNullFieldNames;
	}

	@Override
	public BankModel createModelInstance() {
		return new BankModel();
	}


	@Override
	public String getCodeFieldName() {
		return BankModel.CODE;
	}


}
